package com.offcn.sellergoods.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser;
import com.offcn.dongyimaicommon.entity.PageResult;
import com.offcn.dongyimaicommon.entity.Result;
import com.offcn.dongyimaicommon.entity.StatusCode;
import com.offcn.sellergoods.pojo.Brand;
import com.offcn.sellergoods.service.BrandService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("brand")
@CrossOrigin
public class BrandController {
    @Resource
    private BrandService brandService;
     @GetMapping
    public Result<List<Brand>>  findAll(){
        List<Brand> all = brandService.findAll();
        return new Result<List<Brand>>(true, StatusCode.OK,"查询成功",all);
    }
    @GetMapping("/{id}")
    public Result<Brand> getById(@PathVariable Long id){
         return new Result<Brand>(true,StatusCode.OK,"查询成功",brandService.findId(id));
    }


    @PostMapping
    public Result<Brand> add(@RequestBody Brand brand){
        try {
            brandService.add(brand);
            return new Result<Brand>(true,StatusCode.OK,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result<Brand>(false,StatusCode.REERROR,"添加失败");
    }
    @PutMapping
    public Result<Brand> update(@RequestBody Brand brand){
        try {
            brandService.update(brand);
            return new Result<Brand>(true,StatusCode.OK,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result<Brand>(false,StatusCode.REMOTEERROR,"修改失败");
    }

    @DeleteMapping("/{id}")
    public Result<Brand> remove(@PathVariable Long id){
        try {
            brandService.remove(id);
            return new Result<Brand>(true,StatusCode.OK,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result<Brand>(false,StatusCode.REERROR,"删除成功");
    }

     @PostMapping("/{page}/{size}")
    public PageResult<Brand> pageList(@RequestBody Brand brand,@PathVariable int page,@PathVariable int size){
         PageResult<Brand> brandPageResult = brandService.pageList(brand, page, size);
         return brandPageResult;
     }


}
